package codetop.microsoft.T98;

import util.TreeNode;

/**
 * @Author: 18362
 * @Create: 2022-08-14 8:33:42 星期日
 */
class Solution {
    public boolean isValidBST(TreeNode root) {
        if (root == null)
            return true;
        if (root.left != null) {
            TreeNode left = root.left;
            while (left.right != null)
                left = left.right;
            if (root.val <= left.val)
                return false;
        }
        if (root.right != null) {
            TreeNode right = root.right;
            while (right.left != null)
                right = right.left;
            if (root.val >= right.val)
                return false;
        }
        return isValidBST(root.left) && isValidBST(root.right);
    }
}